package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domian.Employee;
import com.itheima.service.EmployyeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployyeService employyeService;

    /**
     * 登录
     *
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //根据用户名查询数据库
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, username);
        //返回数据
        Employee emp = employyeService.getOne(queryWrapper);

        //1.通过用户名获取数据库信息  返回信息进行判断 null就说明用户名不存在
        if (emp == null) {
            return R.error("用户名不存在");
        }

        //2.用户名正确的情况下 数据库就能返回信息 就进行 密码匹配 如果不一致返回密码错误
        String password = emp.getPassword();//数据库的密码  加密了
        String password1 = employee.getPassword();//用户输入的密码  没有加密的
        String password2 = DigestUtils.md5DigestAsHex(password1.getBytes());//进行加密对比
        boolean equals = StringUtils.equals(password, password2);

        if (!equals) {
            return R.error("密码错误");
        }
        //3.进行 状态校验 如果状态未启用 返回  用户已禁用  1启用 0禁用
        int i = emp.getStatus();
        if (i == 0) {
            return R.error("用户已禁用");
        }

        //4.全部通过后就可以进行登录成功
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出功能
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //退出功能就是将  登录的回话进行销毁   有两种方式 1.进行清除数据    2.强行销毁session
//       request.getSession().removeAttribute("employee");//1.进行清除数据
        request.getSession().invalidate();// 2.强行销毁session
        return R.success("退出成功");
    }

    /**
     * 添加成员
     */
    @PostMapping
    public R<String> insert(@RequestBody Employee employee,HttpServletRequest request) {
            //.还需要自己填充 默认密码  还需要进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //获取修改时间和添加时间
      //  employee.setUpdateTime(LocalDateTime.now());
       // employee.setCreateTime(LocalDateTime.now());
        //获取当前操作的用户
        Long id = (Long) request.getSession().getAttribute("employee");
       // employee.setCreateUser(id);
       // employee.setUpdateUser(id);

        employyeService.save(employee);
//        System.out.println(employee);
         return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        //构造分页查询器
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        //构造条件查询器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employyeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 禁用用户  根据id修改员工状态
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){
        Long empId = (Long) request.getSession().getAttribute("employee");
        //更新修改时间
//        employee.setUpdateTime(LocalDateTime.now());
        //修改人信息
       // employee.setUpdateUser(empId);
        employyeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询数据 回显
     */

    @GetMapping("{id}")
    public R<Employee> selectById(@PathVariable("id") Long empId) {
        Employee employee = employyeService.getById(empId);
        return R.success(employee);
    }


}
